<?php
/**
 * ot_cod_fee order-total module
 *
 * @package orderTotal
 * @copyright Copyright 2003-2009 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @copyright Portions Copyright (c) 2002 Thomas Plnkers http://www.oscommerce.at
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: ot_cod_fee.php 14820 2009-11-15 16:17:45Z ajeh $
 */
/**
 * COD-FEE Order Totals Module
 *
 */

  class ot_invoice_fee {
    var $title, $output;

    function ot_invoice_fee() {
      $this->code = 'ot_invoice_fee';
      $this->title = MODULE_ORDER_TOTAL_INVOICE_FEE_TITLE;
      $this->description = MODULE_ORDER_TOTAL_INVOICE_FEE_DESCRIPTION;
      $this->enabled = ((MODULE_ORDER_TOTAL_INVOICE_FEE_STATUS == 'true') ? true : false);
      $this->sort_order = MODULE_ORDER_TOTAL_INVOICE_FEE_SORT_ORDER;

      $this->output = array();
    }

    function process() {
      global $order, $currencies, $invoice_fee_cost, $invoice_fee_country, $shipping;

      if ($this->enabled == 'true') {
        //Will become true, if invoice_fee can be processed.
        $invoice_fee_country = false;

        //check if payment method is invoice.

        if ($_SESSION['payment'] == 'invoice' ) {
          //process installed shipping modules
          $invoice_fee_zones = split("[:,]", MODULE_ORDER_TOTAL_INVOICE_FEE);

            for ($i = 0; $i < count($invoice_fee_zones); $i++) {
              if ($invoice_fee_zones[$i] == $order->delivery['country']['iso_code_2']) {
                  $invoice_fee_cost = $invoice_fee_zones[$i + 1];
                  $invoice_fee_country = true;
                  //print('match' . $i . ': ' . $invoice_fee_cost);
                  break;
                } elseif ($invoice_fee_zones[$i] == '00') {
                  $invoice_fee_cost = $invoice_fee_zones[$i + 1];
                  $invoice_fee_country = true;
                  //print('match' . $i . ': ' . $invoice_fee_cost);
                  break;
                } else {
                  //print('no match');
                }
              $i++;
            }
          } else {
            //Invoice selected, but no shipping module which offers Invoice
          }

        if ($invoice_fee_country) {
          $invoice_fee_tax_address = zen_get_tax_locations();
          $tax = zen_get_tax_rate(MODULE_ORDER_TOTAL_INVOICE_FEE_TAX_CLASS, $invoice_fee_tax_address['country_id'], $invoice_fee_tax_address['zone_id']);
          $order->info['total'] += $invoice_fee_cost;
          if ($tax > 0) {
            $tax_description = zen_get_tax_description(MODULE_ORDER_TOTAL_INVOICE_FEE_TAX_CLASS, $invoice_fee_tax_address['country_id'], $invoice_fee_tax_address['zone_id']);
            $order->info['tax'] += zen_calculate_tax($invoice_fee_cost, $tax);
            $order->info['tax_groups'][$tax_description] += zen_calculate_tax($invoice_fee_cost, $tax);
            $order->info['total'] += zen_calculate_tax($invoice_fee_cost, $tax);
            if (DISPLAY_PRICE_WITH_TAX == 'true') {
              $invoice_fee_cost += zen_calculate_tax($invoice_fee_cost, $tax);
            }
          }

          $this->output[] = array('title' => $this->title . ':',
                                  'text' => $currencies->format($invoice_fee_cost, true,  $order->info['currency'], $order->info['currency_value']),
                                  'value' => $invoice_fee_cost);
        } else {
//Following code should be improved if we can't get the shipping modules disabled, who don't allow OPPKRAV
// as well as countries who do not have invoice_fee
//          $this->output[] = array('title' => $this->title . ':',
//                                  'text' => 'Oppkrav ikke tilgjengelig.',
//                                  'value' => '');
        }
      }
    }

    function check() {
      global $db;
      if (!isset($this->_check)) {
        $check_query = $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_ORDER_TOTAL_INVOICE_FEE_STATUS'");
        $this->_check = $check_query->RecordCount();
      }

      return $this->_check;
    }
    function keys() {
      return array('MODULE_ORDER_TOTAL_INVOICE_FEE_STATUS', 'MODULE_ORDER_TOTAL_INVOICE_FEE_SORT_ORDER', 'MODULE_ORDER_TOTAL_INVOICE_FEE', 'MODULE_ORDER_TOTAL_INVOICE_FEE_TAX_CLASS');
    }

    function install() {
      global $db;
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Display OPPKRAV', 'MODULE_ORDER_TOTAL_INVOICE_FEE_STATUS', 'true', 'Do you want this module to display?', '6', '1','zen_cfg_select_option(array(\'true\', \'false\'), ', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_ORDER_TOTAL_INVOICE_FEE_SORT_ORDER', '950', 'Sort order of display.', '6', '2', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Invoice Fee', 'MODULE_ORDER_TOTAL_INVOICE_FEE', 'NO:30.00,DK:45.00,00:60', 'Fee: &lt;Country code&gt;:&lt;Invoice price&gt;, .... 00 as country code applies for all countries. If country code is 00, it must be the last statement. If no 00:9.99 appears, Invoice charge in foreign countries is not calculated (not possible)', '6', '3', now())");
      $db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_ORDER_TOTAL_INVOICE_FEE_TAX_CLASS', '0', 'Use the following tax class on the Invoice fee.', '6', '25', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");
    }


    function remove() {
      global $db;
      $keys = '';
      $keys_array = $this->keys();
      $keys_size = sizeof($keys_array);
      for ($i=0; $i<$keys_size; $i++) {
        $keys .= "'" . $keys_array[$i] . "',";
      }
      $keys = substr($keys, 0, -1);

      $db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in (" . $keys . ")");
    }
  }
?>
